package datamodel;

import parsers.ParserCommand;

/**
 * A database entry representing a video on the system.
 * 
 * <pre>
 * +-------------+------------------+------+-----+---------+----------------+
 * | Field       | Type             | Null | Key | Default | Extra          |
 * +-------------+------------------+------+-----+---------+----------------+
 * | intid       | int(10) unsigned | NO   | PRI | NULL    | auto_increment | 
 * | title       | varchar(128)     | NO   | MUL |         |                | 
 * | director    | varchar(128)     | NO   | MUL |         |                | 
 * | plot        | text             | YES  |     | NULL    |                | 
 * | rating      | varchar(128)     | NO   |     |         |                | 
 * | inetref     | varchar(32)      | NO   |     |         |                | 
 * | year        | int(10) unsigned | NO   |     | 0       |                | 
 * | userrating  | float            | NO   |     | 0       |                | 
 * | length      | int(10) unsigned | NO   |     | 0       |                | 
 * | showlevel   | int(10) unsigned | NO   |     | 0       |                | 
 * | filename    | text             | NO   |     |         |                | 
 * | coverfile   | text             | NO   |     |         |                | 
 * | childid     | int(11)          | NO   |     | -1      |                | 
 * | playcommand | varchar(255)     | YES  |     | NULL    |                | 
 * | browse      | tinyint(1)       | NO   |     | 1       |                | 
 * | category    | int(10) unsigned | NO   |     | 0       |                | 
 * +-------------+------------------+------+-----+---------+----------------+
 * </pre>
 */

public class Entry {
	String title = "";
	String director = "";
	String plot = "";
	String rating = "";
	String inetref = "";
	String filename = "";
	String coverfile = "";
	String playcommand = "";

	int year = 1900;
	int length = 30;
	int showlevel;
	int chilid;
	int category;
	int intid;

	float userrating = 0;

	boolean browse;

	/* The TV show specific stuff */
	int season, episode;

	boolean isTVShow = false;

	String episodeName;

	/** The TV Show that this entry is an episode of */
	TVShow show;
	/* End TV show stuff */

	ParserCommand parser;

	PossibleMatch possibleMatch;

	/** Whether or not this entry has been processed or not */
	boolean processed = false;

	/** Whether or not the entry has changed from what is in the database */
	private boolean hasChanged = false;

	/**
	 * The match that was chosen by the users as the correct one. This should be
	 * the key for the PossibleValue's value.
	 */
	String chosenMatch = null;

	public Entry( int intid, String title, String director, String plot, String rating, String inetref, int year,
			float userrating, int length, int showlevel, String filename, String coverfile, int chilid,
			String playcommand, boolean browse, int category ) {
		super();
		this.intid = intid;
		this.title = title;
		this.director = director;
		this.plot = plot;
		this.rating = rating;
		this.inetref = inetref;
		this.year = year;
		this.userrating = userrating;
		this.length = length;
		this.showlevel = showlevel;
		this.filename = filename;
		this.coverfile = coverfile;
		this.chilid = chilid;
		this.playcommand = playcommand;
		this.browse = browse;
		this.category = category;
	}

	public PossibleMatch getPossibleMatch() {
		return possibleMatch;
	}

	public void setPossibleMatch( PossibleMatch possibleMatch ) {
		this.possibleMatch = possibleMatch;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle( String title ) {
		this.title = title;
	}

	public String getDirector() {
		return director;
	}

	public void setDirector( String director ) {
		changed();
		this.director = director;
	}

	public String getPlot() {
		return plot;
	}

	public void setPlot( String plot ) {
		changed();
		this.plot = plot;
	}

	public String getRating() {
		return rating;
	}

	public void setRating( String rating ) {
		changed();
		this.rating = rating;
	}

	public String getInetref() {
		return inetref;
	}

	public void setInetref( String inetref ) {
		changed();
		this.inetref = inetref;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename( String filename ) {
		changed();
		this.filename = filename;
	}

	public String getCoverfile() {
		return coverfile;
	}

	public void setCoverfile( String coverfile ) {
		changed();
		this.coverfile = coverfile;
	}

	public String getPlaycommand() {
		return playcommand;
	}

	public void setPlaycommand( String playcommand ) {
		changed();
		this.playcommand = playcommand;
	}

	public int getYear() {
		return year;
	}

	public void setYear( int year ) {
		changed();
		this.year = year;
	}

	public void setYear( String year ) {
		changed();

		try {
			this.year = Integer.parseInt( year );
		} catch ( Exception e ) {
			this.year = 1900;
		}
	}

	public int getLength() {
		return length;
	}

	public void setLength( int length ) {
		changed();
		this.length = length;
	}

	public void setLength( String length ) {
		changed();

		try {
			this.length = Integer.parseInt( length );
		} catch ( Exception e ) {
			// Could not parse length. Giving a default value
			this.length = 30;
		}
	}

	public int getShowlevel() {
		return showlevel;
	}

	public void setShowlevel( int showlevel ) {
		changed();
		this.showlevel = showlevel;
	}

	public int getChilid() {
		return chilid;
	}

	public void setChilid( int chilid ) {
		this.chilid = chilid;
	}

	public int getCategory() {
		return category;
	}

	public void setCategory( int category ) {
		changed();
		this.category = category;
	}

	public float getUserrating() {
		return userrating;
	}

	public void setUserrating( float userrating ) {
		changed();
		this.userrating = userrating;
	}

	public void setUserrating( String userrating ) {
		changed();

		try {
			this.userrating = Float.parseFloat( userrating );
		} catch ( Exception e ) {
			// Could not parse the user rating. Give it a default value
			this.userrating = 0.0F;
		}
	}

	public ParserCommand getParser() {
		return parser;
	}

	public void setParser( ParserCommand parser ) {
		this.parser = parser;
	}

	public String getChosenMatch() {
		return chosenMatch;
	}

	public void setChosenMatch( String chosenMatch ) {
		changed();
		this.chosenMatch = chosenMatch;
	}

	public int getIntid() {
		return intid;
	}

	public void setIntid( int intid ) {
		changed();
		this.intid = intid;
	}

	public boolean isBrowse() {
		return browse;
	}

	public void setBrowse( boolean browse ) {
		changed();
		this.browse = browse;
	}

	public boolean isProcessed() {
		return processed;
	}

	public void setProcessed( boolean processed ) {
		this.processed = processed;
	}

	public int getSeason() {
		return season;
	}

	public void setSeason( int season ) {
		this.season = season;
	}

	public int getEpisode() {
		return episode;
	}

	public void setEpisode( int episode ) {
		this.episode = episode;
	}

	public boolean isTVShow() {
		return isTVShow;
	}

	public void setTVShow( boolean isTVShow ) {
		this.isTVShow = isTVShow;
	}

	public String getEpisodeName() {
		return episodeName;
	}

	public void setEpisodeName( String episodeName ) {
		changed();
		this.episodeName = episodeName;
	}

	private void changed() {
		hasChanged = true;
	}

	public boolean isChanged() {
		return hasChanged;
	}

	public void setChanged( boolean hasChanged ) {
		this.hasChanged = hasChanged;
	}

	/**
	 * Get the TV Show that this entry is an episode of
	 * 
	 * @return
	 */
	public TVShow getShow() {
		return show;
	}

	/**
	 * Set the TV Show that this entry is an episode of
	 * 
	 * @param show
	 */
	public void setShow( TVShow show ) {
		this.show = show;
	}
}
